package hn.cch.spring;

import hn.cch.constant.JdbcConstant;
import hn.cch.dynamic.DynamicDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.FilterType;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@EnableAspectJAutoProxy
@ComponentScan(basePackages = {"hn.cch.*"}
// , excludeFilters={
//         @ComponentScan.Filter(type= FilterType.ANNOTATION, value= EnableWebMvc.class)
// }
)
@Configuration
public class SpringConfiguration {

    private Logger logger = LoggerFactory.getLogger(SpringConfiguration.class);

    @Autowired
    private DataSource master;

    @Autowired
    private DataSource replica;

    @Bean
    public DataSource dataSource(){
        DynamicDataSource dataSource = new DynamicDataSource();
        Map<Object, Object> map = new HashMap<>();
        map.put(JdbcConstant.MASTER, master);
        map.put(JdbcConstant.REPLICA, replica);
        dataSource.setTargetDataSources(map);
        dataSource.setDefaultTargetDataSource(master);
        return dataSource;
    }






}
